﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Newtonsoft.Json;

namespace RN.ERP.Web.Person
{
    public partial class PersonEmpManual : BasePage
    {
        public override bool IsCheckPermission
        {
            get
            {
                return false;
            }
        }

        public string PageTitle
        {
            get
            {
                return ConfigurationManager.AppSettings["sysTitle"];
            }
        }

        /// <summary>
        /// 页面编辑状态
        /// </summary>
        protected int EditStatus
        {
            get;
            set;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetParams();
                SetBtnShowStatus();
                ////异步调用方法
                if (Request.Params["action"] != null)
                {
                    string action = Request.Params["action"];
                    switch (action)
                    {
                        case "Audit":
                            EmpInfoAudit();
                            break;
                    }
                }
            }
        }
        #region
        /// <summary>
        /// 获取传递参数
        /// </summary>
        private void GetParams()
        {
            Guid empGuid = Guid.Empty;
            Guid certGuid = Guid.NewGuid();
            int opt = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["empGuid"]))
            {
                if (Guid.TryParse(Request.QueryString["empGuid"], out empGuid))
                {
                    hdEmpGuid.Value = empGuid.ToString();
                }
            }
            if (!string.IsNullOrEmpty(Request.QueryString["certGuid"]))
            {
                if (Guid.TryParse(Request.QueryString["certGuid"], out certGuid))
                {
                    hdStatus.Value = "1";
                }
            }
            hdEducationGUID.Value = certGuid.ToString();
            hdAreaCode.Value = UserInfo.CurrentArea;
            hdCorpGuid.Value = UserInfo.CorpUserGuid;
            ////可编辑显示状态（1：内容不可编辑，2：所有不可用）
            if (!string.IsNullOrEmpty(Request.QueryString["opt"]))
            {
                int.TryParse(Request.QueryString["opt"], out opt);
            }
            ///证书类别标识非执业手册不可编辑
            if (!string.IsNullOrEmpty(Request.QueryString["manual"]))
            {
                hdManualID.Value = Request.QueryString["manual"];
            }
            else
            {
                opt = 2;
            }
            EditStatus = opt;
        }
        /// <summary>
        /// 设置按钮显示状态
        /// </summary>
        private void SetBtnShowStatus()
        {
            GetCardFlowCodes();
            if (EditStatus == 2)
            {
                certImgDiv.Visible = false;//上传图片
                btnSaveDiv.Visible = false;//保存
            }
        }

        #endregion

        #region
        /// <summary>
        /// 按钮显示状态
        /// </summary>
        private void GetCardFlowCodes()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                //取得证书审核状态
                var result = JsonConvert.DeserializeObject<ModelService.OperationResult>(client.GetCardFlowCodes(UserInfo.CorpUserGuid, hdEmpGuid.Value.Trim(), hdEducationGUID.Value.Trim(), 251201, 7, UserInfo.CurrentArea));
                if (result.Result)
                {
                    List<ModelService.OperationResult> resultList = JsonConvert.DeserializeObject<List<ModelService.OperationResult>>(result.ReturnValue.ToString());
                    if (!resultList[4].Result)//不可上报则不能编辑
                    {
                        EditStatus = 2;
                    }
                    //if (resultList[5].Result || resultList[6].Result)
                    //{
                    //    if (UserInfo.RoleType == "Manager")
                    //    {
                    //        // shenhe.Visible = true;//审核
                    //    }
                    //}

                }
            }
        }
        /// <summary>
        /// 上报审核
        /// </summary>
        private void EmpInfoAudit()
        {
            string empGuid = string.Empty;
            string cartGuid = string.Empty;
            string message = string.Empty;
            if (!string.IsNullOrEmpty(Request.QueryString["empGuid"]))
            {
                empGuid = Request.QueryString["empGuid"];
            }
            if (!string.IsNullOrEmpty(Request.QueryString["certGuid"]))
            {
                cartGuid = Request.QueryString["certGuid"];
            }
            string corpUserGuid = string.Empty;
            string description = string.Empty;
            int optType = 5;
            corpUserGuid = UserInfo.CorpUserGuid;

            if (Request.QueryString["descr"] != null)
            {
                description = Request.QueryString["descr"].ToString();
            }
            if (Request.QueryString["optType"] != null)
            {
                int.TryParse(Request.QueryString["optType"], out optType);
            }
            if (Request.QueryString["message"] != null)
            {
                message = Request.QueryString["message"].ToString();
            }
            ////证书审核--审核方法参数不足，利用日志对象补参数
            string[] arry = cartGuid.Split(',');
            ModelService.User_Log log = new ModelService.User_Log();
            log.IsrtUser = corpUserGuid;
            log.FunctionId = optType.ToString();
            log.IsrtUserName = empGuid;
            log.IsrtUserRole = "251201";//执业手册状态类别
            log.IsrtUserArea = UserInfo.RoleId.ToString();
            log.FunctionType = message;
            log.Notice = description;
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                OutputData(client.EmpInfoAudit(JsonConvert.SerializeObject(arry), true, UserInfo.CurrentArea, JsonConvert.SerializeObject(log), string.Empty, description, false, false));
            }
        }
        /// <summary>
        /// 输出JSON格式数据到页面
        /// </summary>
        /// <param name="dataStr"></param>
        private void OutputData(string dataStr)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(dataStr);
            HttpContext.Current.Response.End();
        }
        #endregion
    }
}